import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { ArticleVO, UserVO } from '@/types'
import { userApi } from '@/api/user'

interface UserState {
  token: string | null
  userInfo: Partial<UserVO> | null
}

const state: UserState = {
  token: localStorage.getItem('token'),
  userInfo: null,
}

export const useUserStore = defineStore('user', () => {
  const userArticles = ref<ArticleVO[]>([])
  const userFavorites = ref<ArticleVO[]>([])

  const fetchUserArticles = async (userId: number) => {
    const response = await userApi.getUserArticles(userId)
    userArticles.value = response.data
  }

  const fetchUserFavorites = async (userId: number) => {
    const response = await userApi.getUserFavorites(userId)
    userFavorites.value = response.data
  }

  return {
    userArticles,
    userFavorites,
    fetchUserArticles,
    fetchUserFavorites,
  }
})
